﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AccesoDatos;
using AccesoDatos.Manejador;
using System.Runtime.InteropServices;

namespace RMA.Pantallas
{
    public partial class frmConsultaAltaPagos : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }
        public frmConsultaAltaPagos()
        {
            InitializeComponent();
        }

        private void frmConsultaAltaPagos_Load(object sender, EventArgs e)
        {
            CargaListado();
        }
        public void CargaListado()
        {
            DataTable dtListado = new DataTable();
            Manejador_Pagos objPagos = new Manejador_Pagos();

            try
            {
                dgvPagos.AutoGenerateColumns = false;
                dtListado = objPagos.RegresaListadoAltaPagos();
                dgvPagos.DataSource = dtListado;
                foreach (DataGridViewColumn ca in dgvPagos.Columns)
                {
                    ca.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }

            }
            catch (Exception ex)
            { 
                
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            frmAltaPagos frmA = new frmAltaPagos();
            AnimateWindow(frmA.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
            frmA.ShowDialog();

            CargaListado();

        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow r in dgvPagos.SelectedRows)
            {
                string strProducto = r.Cells[2].Value.ToString();
                string strTicket = r.Cells[6].Value.ToString();
                frmEditaPagos frmE = new frmEditaPagos(strTicket,strProducto);
                AnimateWindow(frmE.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
                frmE.ShowDialog();
                CargaListado();


            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            bool Elimina = false;

            Manejador_Pagos objPagos = new Manejador_Pagos();

            if (MessageBox.Show("Desea eliminar el Registro", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    if (dgvPagos.SelectedRows.Count == 1)
                    {
                        foreach (DataGridViewRow r in dgvPagos.SelectedRows)
                        {
                            Elimina = objPagos.EliminaAltaPagos(int.Parse(r.Cells[7].Value.ToString()));

                            if (Elimina)
                            {
                                MessageBox.Show("Registro Eliminado Correctamente", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                CargaListado();
                            }
                            else
                            {
                                MessageBox.Show("Error al Eliminar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            

            
        }
    }
}
